package edu.hgnc.tx.annotation.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public class BookDao {

	private static final Log logger = LogFactory.getLog(BookDao.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void test(){
		logger.debug("bookDao --- --------------------");
		List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from test_db.table_name");
		logger.debug("=========================================================");
		for (Map<String, Object> e : mapList) {
			logger.debug(e);
		}
		logger.debug("=========================================================");
	}

	/**
	 * 减去某个账户余额
	 * @param userName
	 * @param price
	 * @return
	 */
	@Transactional
	public Integer updateBalance(String userName, Integer price){
		String sql = "update account set balance = balance-? where username=?";
		return jdbcTemplate.update(sql,price,userName);
	}

	/**
	 * 获取价格
	 * @param id
	 * @return
	 */
	@Transactional
	public Integer getPrice(Integer id){
		String sql = "select price from book where id=?";
		return jdbcTemplate.queryForObject(sql,Integer.class,id);
	}

}
